package org.nanotek.tika.tests.writers;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.tika.io.TikaInputStream;
import org.nanotek.cms.domain.lucene.Index;
import org.nanotek.dao.DAO;
import org.nanotek.lucene.index.manage.LuceneIndexWriteService;
import org.nanotek.lucene.index.search.AnalyzerType;
import org.nanotek.lucene.index.search.IndexType;
import org.nanotek.tika.fs.DirectoryScanner;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobExecutionNotRunningException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sun.syndication.feed.atom.Link;

public class DirectoryScannerTest {

	private static ClassPathXmlApplicationContext ctx;

	/**
	 * @param args
	 * @throws IOException 
	 * @throws JobParametersInvalidException 
	 * @throws JobInstanceAlreadyCompleteException 
	 * @throws JobRestartException 
	 * @throws JobExecutionAlreadyRunningException 
	 * @throws NoSuchJobException 
	 * @throws JobExecutionNotRunningException 
	 * @throws NoSuchJobExecutionException 
	 */
	public static void main(String[] args) throws Exception  {
		
		ctx = new ClassPathXmlApplicationContext(new String[] {"robot-index-xml/base-tika-indexer-xml-context.xml"});
		//Create Index File and retain index reference to pass to the indexWriter Service.
		File idxFile = (File) ctx.getBean("indexFilePath");
		Index index = new Index(); 
		Link location = new Link(); 
		location.setTitle("Base Index Writer Config Test"); 
		location.setHref(idxFile.getPath());
		index.setLocation(location); 
		index.setIndexAlias("A simple base index alias"); 
		index.setIndexType(IndexType.NMAPDIRECTORY);
		index.setAnalyzerType(AnalyzerType.STANDARD);
		DAO<Link> linkDAO = (DAO<Link>)ctx.getBean("linkDAO");
		DAO<Index> indexDAO = (DAO<Index>) ctx.getBean("indexDAO"); 
		linkDAO.update(location);
		indexDAO.update(index);
		
		DirectoryScanner bean = (DirectoryScanner) ctx.getBean("baseDirectoryScanner");
		bean.scan(index);
//		FSDirectory directory = (FSDirectory) idxWriter.getDirectory(); 
//		File file = directory.getDirectory();
		System.out.println(bean.toString());
	}

}
